/**
 * 
 */
package com.sci.web.daoimpl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.sci.web.bean.Enterprise_Base;
import com.sci.web.bean.Options;
import com.sci.web.bean.Permission;
import com.sci.web.bean.PersmissionPojo;
import com.sci.web.bean.RoleTree;

/**
 * @author  BaiFuyou
 * @date: Nov 11, 2011 5:36:13 PM
 */
public class SystemManageDao extends SqlMapClientDaoSupport implements com.sci.web.dao.SystemManageDao {

	@Override
	public void updateSystemReportTime(final Options o) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {

			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				// TODO Auto-generated method stub
				executor.startBatch();				
					executor.delete("systemmanage.deletealloptions");
					executor.insert("systemmanage.insertnewoptions",o);
				executor.executeBatch();
				return null;
			}
		});
	}

	@Override
	public List<Options> getOneOptions() {
		// TODO Auto-generated method stub
		return super.getSqlMapClientTemplate().queryForList("systemmanage.getoneoptions");
	}

	@Override
	public List<Enterprise_Base> getWaitingCheckEnterprise(int cid) {
		// TODO Auto-generated method stub
		return super.getSqlMapClientTemplate().queryForList("systemmanage.getallwaitingenbycityid",cid);
	}

	@Override
	public List<RoleTree> getAllRoleTree() {
		// TODO Auto-generated method stub
		return super.getSqlMapClientTemplate().queryForList("systemmanage.getAllRoleTree");
	}

	@Override
	public List<PersmissionPojo> getPermissionPojoByRid(int roleid) {
		// TODO Auto-generated method stub
		List<PersmissionPojo> list = null;
		Permission p = (Permission)super.getSqlMapClientTemplate().queryForObject("systemmanage.getallrolepermission",roleid);
		if (p == null) {//此时没有权限
//			list = com.sci.web.action.util.PermissionUtil.getAllNeverAuthorizedByPermissionifPnull(p);
			list = new ArrayList<PersmissionPojo>();
		} else {
			list = com.sci.web.action.util.PermissionUtil.getAllAuthorizedByPermission(p);
		}
		System.out.println(list);
		return list;
	}

	@Override
	public List<PersmissionPojo> getUnAuthorizedPermissionPojoByRid(int roleid) {
		// TODO Auto-generated method stub
		List<PersmissionPojo> list = new ArrayList<PersmissionPojo>();
		Permission p = (Permission)super.getSqlMapClientTemplate().queryForObject("systemmanage.getallrolepermission",roleid);
		if (p == null) {//此时没有权限
//			list = com.sci.web.action.util.PermissionUtil.getAllNeverAuthorizedByPermissionifPnull(p);
			list = com.sci.web.action.util.PermissionUtil.getAllNeverAuthorizedByPermissionifPnull(p);
		} else {
			list = com.sci.web.action.util.PermissionUtil.getAllNeverAuthorizedByPermission(p);
		}
		return list;
	}

	@Override
	public boolean addNewPermission_update(Permission p) {
		// TODO Auto-generated method stub
		return (Integer)super.getSqlMapClientTemplate().update("systemmanage.addnewpermission_update",p)>0?true:false;
	}

	@Override
	public boolean deleteOnePermission_update(Permission p) {
		// TODO Auto-generated method stub
		return (Integer)super.getSqlMapClientTemplate().update("systemmanage.addnewpermission_update",p)>0?true:false;
	}

	@Override
	public Permission getPermission(int roleid) {
		// TODO Auto-generated method stub
		return (Permission)super.getSqlMapClientTemplate().queryForObject("systemmanage.getallrolepermission",roleid);
	}

}
